package icegps.com.pingdj.map.draw;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;

import icegps.com.pingdj.App;
import icegps.com.pingdj.map.bean.MapStatus;

import static icegps.com.pingdj.map.MapManager.windowHeight;
import static icegps.com.pingdj.map.MapManager.windowWidth;
import static icegps.com.pingdj.map.count.ScreenCount.getScreenCxByTx;
import static icegps.com.pingdj.map.count.ScreenCount.getScreenCyByTy;


/**
 * Created by 111 on 2018/3/5.
 */

public class DrawLineCopy {
    private Paint linePint;
    private MapStatus mapStatus;
    private int DIST = 50;
    private float multiple;

    public DrawLineCopy(MapStatus mapStatus) {
        this.mapStatus = mapStatus;
        linePint = new Paint();
        linePint.setColor(Color.parseColor("#663F51B5"));
        linePint.setPathEffect(new DashPathEffect(new float[]{5, 5}, 0));
    }

    public void drawLine(Canvas canvas) {
        multiple = mapStatus.getMultiple();
        drawHLine(canvas);
        drawVLine(canvas);
    }

    //取竖向屏幕中心为基准点
    float hStartX = App.getInstance().getWindowHeight() / 2;

    /**
     * @param canvas 以竖向屏幕中心点为基准点,绘制一条横线,尔后以基准点为中心,向上下扩散
     */
    private void drawHLine(Canvas canvas) {
        int countX = (windowHeight / DIST + 4) / 2;
//        float startX = -mapStatus.getSumTx() * (1.0f / multiple);
        float startX = -mapStatus.getSumTx();
        float stopX = startX + windowWidth;
        //若偏移距离超过一个gap则将基准点重新设置为当前屏幕中心点的y 如此达到无限重复
        boolean b = Math.abs(mapStatus.getSumTy() - mapStatus.getOldSumTy()) >= DIST * multiple;
        if (b) {
            hStartX = getScreenCyByTy(mapStatus) * (1.0f / multiple);
            mapStatus.setOldSumTy(mapStatus.getSumTy());
        }
        float stopY = hStartX;
        for (int i = 0; i < countX; i++) {
            canvas.drawLine(startX, hStartX + i * DIST, stopX, stopY + i * DIST, linePint);//左
        }
        for (int i = 1; i <= countX; i++) {
            canvas.drawLine(startX, hStartX + -i * DIST, stopX, stopY + -i * DIST, linePint);//右
        }
    }


    //取横向屏幕中心为基准点
    float vStartX = App.getInstance().getWindowWidth() / 2;

    /**
     * @param canvas 以横向屏幕中心点为基准点,绘制一条竖线,尔后以基准点为中心,向左右扩散
     */
    private void drawVLine(Canvas canvas) {
        int countY = (windowWidth / DIST + 4) / 2;
//        float startY = -mapStatus.getSumTy() * (1.0f / multiple);
        float startY = -mapStatus.getSumTy();
        float stopY = startY + windowHeight;
        //若偏移距离超过一个gap则将基准点重新设置为当前屏幕中心点的x 如此达到无限重复
        boolean b = Math.abs(mapStatus.getSumTx() - mapStatus.getOldSumTx()) >= DIST * multiple;
        if (b) {
            vStartX = getScreenCxByTx(mapStatus) * (1.0f / multiple);
            mapStatus.setOldSumTx(mapStatus.getSumTx());
        }
        float stopX = vStartX;
        for (int i = 0; i < countY; i++) {
            canvas.drawLine(vStartX + i * DIST, startY, stopX + i * DIST, stopY, linePint);//右
        }
        for (int i = 1; i <= countY; i++) {
            canvas.drawLine(vStartX + -i * DIST, startY, stopX + -i * DIST, stopY, linePint);//左
        }
    }

}
